/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
 $("input[name=txt_code],input[name=txt_activity_score]").keypress(function(event){
       
        if(event.which && (event.which < 48 || event.which > 57 ) ){
            event.preventDefault();
        }
    });
    
   $('<div  id="wiged-area"></div>').appendTo("body");
});

function _submit_add_teacher(){
    if(check_form_teacher()){
        var form = $("form[name=frm]").serializeArray();
        $.post(base_url + "teacher/add_teacher_data",form,function(xml){
            var p_xml = $.parseXML(xml);
            if($(p_xml).find("status:eq(0)").attr("q") == "true"){
                alert("เพิ่มข้อมูลเสร็จเรียบร้อยแล้ว");
                $("form[name=frm]")[0].reset();
            }else{
                alert("ผิดพลาดไม่สามารถเพิ่มข้อมูลได้");
            }
        });
    }
}

function check_form_teacher(){
    
    if($("input[name=txt_name]:eq(0)").val() == ""){
          alert("กรุณากรอกชื่ออาจารย์");
          return false;
    }else if( $("input[name=txt_code]:eq(0)").val() == "" ){
          alert("กรุณากรอกรหัสอาจารย์");
          return false;
    }else if( $("input[name=email]:eq(0)").val() == "" ){
          alert("กรุณากรอกข้อมูลอีเมล");
          return false;
    }
    return true;
}

function _search_teachers(){
    var keyword = $("input[name=txt_search]:eq(0)").val();
    $.post(base_url + "teacher/search_teachers",$("form[name=frm]").serializeArray(),function(data){
        //alert(data);
        $("#area-display").html("").html(data);
    });
}

function _edit_teacher( teacher_id ){
    $.post(base_url + "teacher/get_dialog_edit_teacher",{"teacher_id" : teacher_id ,"rand" : Math.random()},function(data){
        $("#wiged-area").html("");
        $(data).appendTo("#wiged-area").draggable({"handle" : ".skin-title"}); 
        setPostion("#dialog_edit_teacher","fixed").hide().fadeIn(200);
    });
}

function _update_teacher(teacher_id){
    $.post(base_url + "teacher/update_teacher", $("form[name=frm_edit_dialog]").serializeArray() ,function(data){
       $('#dialog_edit_teacher').fadeOut(200);
       _search_teachers();
    });
}

function setPostion( object_dom,pos){
    var obj_dom = $(object_dom);
    var _w   = $("body").width();
    var _h   = $("body").height();
    var _this_w = obj_dom.width();
    var _this_h = obj_dom.height();
    obj_dom.css({
        "position" : pos,
        "top"  :  "100px",
        "left" : ((_w/2)-(_this_w/2)) + "px"
    });
    if(pos != "fixed"){
        $("html,body").animate({"scrollTop" : "0px"},1000);
    }
    return obj_dom;
}


